<?php
/* * *********************************************************** */
/* * ********************** Classe autorDao ********************* */
/* * ************ Desenvolvido por Antônio Ezequiel ************** */
/* * *********************************************************** */

class AutorDao extends Autor {

    public function adicionarAutor() {

        global $configurator;

        parent::conectarBD();
        
        $queri = parent::query("insert into tbl_autor (aut_nome, aut_email, aut_site, aut_info)
                                values ('$this->aut_nome', '$this->aut_email', '$this->aut_site', '$this->aut_info')");

        if ($queri) {
            $id = parent::get_insert_id();
            parent::desconectarBD();
            return $id;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function listarAutorNome($nome) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("select aut_codigo as codigo from tbl_autor where aut_ativo = 1 and aut_nome like '$nome'");

        if ($queri) {
            $dados = $queri->fetch_assoc();
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function listarAutorAutoComplite($nome) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("select aut_nome as nome, aut_codigo as codigo from tbl_autor where aut_ativo = 1 and aut_nome like '%$nome%' order by aut_nome asc");

        if ($queri) {
            $dados = parent::fetch_rows($queri);
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

     public function atualizarAutor($codigo) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("update tbl_autor set aut_nome = '$this->aut_nome', aut_email = '$this->aut_email',
                                aut_info = '$this->aut_info', aut_site = '$this->aut_site' where aut_codigo = '$codigo'");

        if ($queri) {
            parent::desconectarBD();
            return true;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function deletarAutor($codigo) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("update tbl_autor set aut_ativo = '0' where aut_codigo = '$codigo'");

        if ($queri) {
            parent::desconectarBD();
            return true;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function listarAutoresPaginado($inicio, $porPagina) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("select aut_nome as nome, aut_codigo as codigo, aut_site as site, aut_email as email, aut_info as info
                                from tbl_autor where aut_ativo = '1' order by aut_nome asc limit $inicio, $porPagina");

        if ($queri) {
            $dados = parent::fetch_rows($queri);
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function listarAutores() {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("select aut_nome as nome, aut_codigo as codigo from tbl_autor where aut_ativo = '1' order by aut_nome asc");

        if ($queri) {
            $dados = parent::fetch_rows($queri);
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function listarAutoresMax() {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("select aut_nome as nome, aut_codigo as codigo from tbl_autor where aut_ativo = '1' order by aut_codigo desc limit 0, 1");

        if ($queri) {
            $dados = parent::fetch_rows($queri);
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function listarAutoresPaginadoAjax($ini, $codigo, $inicio, $porPagina) {

        global $configurator;

        parent::conectarBD();
        
        $queri = parent::query(sprintf("select aut_nome as nome, aut_codigo as codigo, aut_site as site, aut_email as email, aut_info as info
                                from tbl_autor where %s %s %s %s LIMIT $inicio, $porPagina", $ini, $codigo, $this->aut_nome, $this->aut_email));

        if ($queri) {
            $dados = parent::fetch_rows($queri);
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function listarAutor($codigo) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("select aut_nome as nome, aut_codigo as codigo, aut_site as site, aut_email as email, aut_info as info
                                from tbl_autor where aut_codigo ='$codigo' and aut_ativo = '1'");

        if ($queri) {
            $dados = $queri->fetch_assoc();
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function totalPaginas() {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("select count(aut_codigo) as quantidade from tbl_autor where aut_ativo = '1'");

        if ($queri) {
            $dados = $queri->fetch_assoc();
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function totalPaginasAjax($ini, $codigo) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query(sprintf("select count(*) as totRegistros from tbl_autor
                                        where %s %s %s %s", $ini, $codigo, $this->aut_nome, $this->aut_email));

        if ($queri) {
            $dados = $queri->fetch_assoc();
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }
}

?>
